package com.fangcang.pdf;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.pdf.BaseFont;

public class PdfUtils {
	private static final Log log = LogFactory.getLog(PdfUtils.class);

	public static String handleHtml(String content) {
		String docType = "<!DOCTYPE html > ";
		String handlContent = new String(docType + (content.replace("META", "meta").replace("BODY", "body")
				.replace("HTML", "html").replace("&rsquo;", "’")// 字符 [’]
				.replace("&ldquo;", "“")// 字符[“]
				.replace("&rdquo", "”")// 字符[”]
				.replace("&amp;", "&#38;")// 字符 [&]
				.replace("&nbsp;", "&#160;")// 空格占位符
				.replace("&pound;", "&#163;")// 字符[£]
				.replace("&yen;", "&#165;")// 字符[¥]
				.replace("&middot;", "&#183;")// 字符[·]
				.replace("<td >", "<td>").replace("charset=utf-8\">", "charset=utf-8\"/>")
				.replace("font-family:Tahoma, Geneva, sans-serif;", "font-family:SimSun;").replace("&quot;", "\"\"")
				.replace("align=\"right\"", "").replace("gif\">", "gif\"></img>").replace("png\">", "png\"></img>")
				.replace("jpg\">", "jpg\"></img>").replace("<br>", "<br></br>")
				.replace("<col width=\"12%\">", "<col width=\"12%\" ></col>")
				.replace("<col width=\"\">", "<col width=\"\" ></col>")
				.replace("<col width=\"80\">", "<col width=\"80\" ></col>")
				.replace("<col width=\"70\">", "<col width=\"70\" ></col>")
				.replace("<col width=\"60\">", "<col width=\"60\" ></col>")
				.replace("<col width=\"100\">", "<col width=\"100\" ></col>")
				.replace("<col width=\"120\">", "<col width=\"120\" ></col>")
				.replace("<col width=\"140\">", "<col width=\"140\" ></col>")
				.replace("<col width=\"160\">", "<col width=\"160\" ></col>")
				.replace("<col width=\"190\">", "<col width=\"190\" ></col>")
				.replace("<col width=\"300\">", "<col width=\"300\" ></col>")
				.replace("<col width=\"15%\">", "<col width=\"15%\" ></col>")
				.replace("<col width=\"8%\">", "<col width=\"8%\" ></col>")
				.replace("<col width=\"6%\">", "<col width=\"6%\" ></col>")
				.replace("<col width=\"25%\">", "<col width=\"25%\" ></col>")
				.replace("<col width=\"10%\">", "<col width=\"10%\" ></col>")
				.replace("<col width=\"7%\">", "<col width=\"7%\" ></col>").replace("font-family: \"黑体\";", "")));
		return handlContent;
	}

	public static byte[] makePdfFile(String content) {
		byte[] byteArray = null;
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		try {
			String handleHtml = PdfUtils.handleHtml(content);
			log.info("handleHtml=======" + handleHtml);
			ITextRenderer renderer = new ITextRenderer();
			URL url = PdfUtils.class.getClassLoader().getResource("simsun.ttc");
			log.info("*******************Print simsun.ttc Path: ************************" + url.getPath());
			String filePath = url.getPath();
//			String path = ExportManageController.class.getResource("/").getPath();
//			String filePath = path + "simsun.ttc";
			
			//renderer.setDocumentFromString(handleHtml);
			renderer.setDocument(new File("d://aa.html"));
			// 添加宋体
			renderer.getFontResolver().addFont(filePath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
			log.info("---------------------2 PdfUtils 取得预览内容：");
			renderer.layout();
			log.info("---------------------3 PdfUtils 取得预览内容：");
			renderer.createPDF(output);
			log.info("---------------------4 PdfUtils 取得预览内容：");
			byteArray = output.toByteArray();
		} catch (Exception e) {
			log.error("PdfUtils 导出pdf失败" + e);
		} finally {
			try {
				output.close();
			} catch (IOException e) {
				log.error("PdfUtils 关闭IO失败" + e);
			}
		}
		return byteArray;
	}
	
	
	public static void main(String[] args) throws IOException {
		StringBuffer sb = new StringBuffer();
//		BufferedReader fr = new BufferedReader(new InputStreamReader(new FileInputStream("d://GroupDistributorBookFax.xsl")));
//		String line = null;
//		while((line =fr.readLine() )  != null) {
//			sb.append(line);
//		}
		
		byte[] bytes = makePdfFile(sb.toString());
		FileOutputStream fos = new FileOutputStream(new File("d://1.pdf"));
		fos.write(bytes);
		fos.flush();
		fos.close();
//		fr.close();
	}
}
